/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aeronef.builder;

import aeronef.appStates.GameState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 *
 * @author Hagan
 */
public abstract class AbstractBuilder {

    protected GameState gameState;
    protected AssetManager assetManager;
    protected AppStateManager stateManager;
    
    protected Document definition;
    
    public AbstractBuilder(GameState gameState) {
        this.gameState = gameState;
        this.assetManager = gameState.getAssetManager();
        this.stateManager = gameState.getStateManager();
    }

    protected Document buildDocument(String definition) {
        Document doc = null;
        try {
            InputSource inputSource = new InputSource(new java.io.StringReader(definition));
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            doc = dBuilder.parse(inputSource);
            doc.getDocumentElement().normalize();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return doc;
    }

    protected List<Element> getElementsByTagName(Element element, String tagName) {
        List<Element> elements = new ArrayList<Element>();
        NodeList nList = element.getElementsByTagName(tagName);
        for (int temp = 0; temp < nList.getLength(); temp++) {
            org.w3c.dom.Node nNode = nList.item(temp);
            if (nNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                elements.add((Element) nNode);
            }
        }
        return elements;
    }
    
    protected static String getTagValue(String sTag, Element eElement) {
        
        NodeList matchList = eElement.getElementsByTagName(sTag);
        if (matchList.getLength() < 1) { 
            System.out.println("Could not find element " + sTag);
            return null; 
        }
        
        NodeList childList = matchList.item(0).getChildNodes();
        if (childList.getLength() < 1) { 
            System.out.println("Could not find any children of element " + sTag);
            return null;
        }
        Node nValue = (Node) childList.item(0);
        return nValue.getNodeValue();
    }
    
    protected static Float getTagValueAsFloat(String sTag, Element eElement) {

        String valueAsString = getTagValue(sTag, eElement);
        if (valueAsString == null) { return null; }
        Float parsedFloat = null;
        try {
            parsedFloat = Float.parseFloat(valueAsString);
            return parsedFloat;
        } catch (NumberFormatException nfe) {
            System.out.println("Error parsing float from sTag " + sTag);
        }
        return null;
    }
}
